﻿
namespace HSS.Xaml.UI.Controls
{
	#region Using Directives
	using System.Windows;
	using System.Windows.Controls;
	using HSS.ComponentModel;
	#endregion

	#region ObjectValueEditor
	/// <summary>
	/// Editor for objects when no other editors are available.
	/// </summary>
	public class ObjectValueEditor : PropertyGridEditor
	{
		#region Fields
		ChildWindow dialog;
		Button button;
		bool isReadOnly;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="descriptor">The <see cref="HSSPropertyDescriptor"/> being edited.</param>
		public ObjectValueEditor(HSSPropertyDescriptor descriptor)
			: base(descriptor)
		{
			Guard.ArgumentNotNull(descriptor, "descriptor");
			this.isReadOnly = descriptor.IsReadOnly || !descriptor.CanWrite;
			this.button = new Button();
			this.button.Margin = new System.Windows.Thickness(0);
			this.button.Padding = new System.Windows.Thickness(4, 1, 4, 1);
			this.button.Content = "...";
			this._supportsEditDialog = descriptor.Property.PropertyType.GetProperties().Length > 0;
		}
		#endregion

		#region Properties
		private LabelTextBox Control
		{
			get { return (LabelTextBox)base.InlineEditControl; }
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Gets the button that will be clicked to show the Dialog Editor.
		/// </summary>
		/// <returns>The <see cref="System.Windows.Controls.Button"/> to be rendered.</returns>
		public override System.Windows.Controls.Button GetDialogEditButton()
		{
			return button;
		}
		/// <summary>
		/// Gets if this editor support showing a Dialog Editor.
		/// </summary>
		public override bool SupportsEditDialog
		{
			get
			{
				return _supportsEditDialog && null != this.PropertyEntry.CurrentValue;
			}
		} bool _supportsEditDialog;
		/// <summary>
		/// Shows the Dialog Editor.
		/// </summary>
		protected override void ShowEditDialogInternal()
		{
			var propertyGrid = new PropertyGrid()
			{
				ForceReadOnly = this.PropertyEntry.Descriptor.IsReadOnly,
				//DefaultLabelWidth = new GridLength(150),
				//ToolbarBorderThickness = new Thickness(0),
				//BorderThickness = new Thickness(0, 1, 0, 1),
				//DescriptionBorderThickness = new Thickness(0, 1, 0, 0),
				MinWidth = 500,
				MinHeight = 400
			};
			//this.dialog = new ChildWindow() { Title = this.PropertyEntry.CurrentValue };
			//this.dialog = new ChildWindow() { Title = this.PropertyEntry.Name };
			if (null != this.dialog)
				this.dialog.Close();
			this.dialog = new ChildWindow() { Title = this.PropertyEntry.Name + " - " + this.PropertyEntry.CurrentValue };
			this.dialog.Content = propertyGrid;
			this.dialog.Closed += new System.EventHandler(dialog_Closed);
			propertyGrid.SelectedObject = this.PropertyEntry.CurrentValue;
			this.dialog.Show();
		}

		void dialog_Closed(object sender, System.EventArgs e)
		{
			this.dialog.Closed -= dialog_Closed;
			this.dialog = null;
			if (this.isReadOnly)
				this.CancelEdit();
			else
				this.EndEdit();
		}
		/// <summary>
		/// Gets the controls editing value.
		/// </summary>
		/// <returns>The value.</returns>
		protected override object GetControlValue()
		{
			return this.PropertyEntry.CurrentValue;
		}
		/// <summary>
		/// Updates the editor with the new value.
		/// </summary>
		/// <param name="newValue">The new value.</param>
		protected override void UpdateInlineEditControl(object newValue)
		{
			var ks = newValue as string;
			if (null != ks)
				this.Control.Text = ks;
			else
				this.Control.Text = this.ValueToString(newValue);
		}
		/// <summary>
		/// Creates the Inline Editor control.
		/// </summary>
		/// <returns>The control as a FrameworkElement.</returns>
		protected override FrameworkElement CreateInlineEditControl()
		{
			return new LabelTextBox
			{
				Margin = new Thickness(0),
				BorderThickness = new Thickness(0),
				HorizontalAlignment = HorizontalAlignment.Stretch,
				IsReadOnly = true
			};
		}

		#endregion
	}
	#endregion
}